package com.lei.service.impl;

import com.lei.dao.StaffDao;
import com.lei.dao.impl.StaffDaoImpl;
import com.lei.entity.Staff;
import com.lei.service.StaffService;
import com.lei.util.PageBean;
import com.lei.util.TransBean;

import java.util.List;
import java.util.Map;

/**
 * @author Lei Chu
 * @Date 2020/10/16 18:16
 */
public class StaffServiceImpl implements StaffService {

    private final StaffDao staffDao = new StaffDaoImpl();

    @Override
    public Staff checkLogin(Staff staff) {
        return staffDao.checkLogin(staff);
    }

    @Override
    public PageBean getAllStaff(Staff staff) {
        PageBean<Staff> page = new PageBean<>();
        int count = staffDao.getCount(staff);
        if (count == 0) {
            return page;
        }
        page.setCount((long) count);
        List<Staff> staffList = staffDao.getAllStaff(staff);
        for (Staff staff1 : staffList) {
            staff1.setAge();
        }
        page.setData(staffList);
        return page;

    }

    @Override
    public int getLastStaffId() {
        int lastStaffId = staffDao.getLastStaffId();
        lastStaffId++;
        return lastStaffId;
    }

    @Override
    public int addStaff(Staff staff) {
        if (staff.getId() == null) {
            //设置创建时间
            staff.setCreatedTime();
            // 设置初始密码
            staff.setPassword();
            return staffDao.addStaff(staff);

        } else {

            return staffDao.updateStaff(staff);
        }
    }

    @Override
    public int updatePassword(Staff staff) {
        return staffDao.updatePassword(staff);
    }

    @Override
    public int deleteStaff(int id) {
        return staffDao.deleteStaff(id);
    }

    @Override
    public PageBean<Staff> getAllResignedStaff(Staff staff) {
        PageBean<Staff> page = new PageBean<>();
        int count = staffDao.getResignedCount(staff);
        if (count == 0) {
            return page;
        }
        page.setCount((long) count);
        List<Staff> staffList = staffDao.getAllResignedStaff(staff);
        for (Staff staff1 : staffList) {
            staff1.setAge();
        }
        page.setData(staffList);
        return page;
    }

    @Override
    public int staffResigned(String id) {
        Staff staff = new Staff();
        staff.setCreatedTime();
        return staffDao.staffResigned(id, staff.getCreatedTime());
    }

    @Override
    public int staffCreated(String id) {
        Staff staff = new Staff();
        staff.setCreatedTime();
        return staffDao.staffCreated(id, staff.getCreatedTime());
    }

    @Override
    public Staff getNowStaffById(Staff nowStaff) {
        List<Map> list = staffDao.getNowStaffById(nowStaff.getId());
        return (Staff) TransBean.populate(Staff.class,list).get(0);
    }

    @Override
    public int updateStaffPhoneAndAdderss(Staff staff) {
        return staffDao.updateStaffPhoneAndAdderss(staff);
    }

    @Override
    public int updateUrl(String url, Integer id) {
        return staffDao.updateUrl(url,id);
    }
}
